package login;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

public class NewPassword extends HttpServlet {
	public void wrong1() {
		String msg = "用户名错误！";
		int type = JOptionPane.YES_NO_CANCEL_OPTION;
		String title = "信息提示";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}

	public void wrong2() {
		String msg = "原密码错误！";
		int type = JOptionPane.YES_NO_CANCEL_OPTION;
		String title = "信息提示";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}

	public void wrong3() {
		String msg = "新密码错误！";
		int type = JOptionPane.YES_NO_CANCEL_OPTION;
		String title = "信息提示";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}

	public void wrong4() {
		String msg = "重复密码错误！";
		int type = JOptionPane.YES_NO_CANCEL_OPTION;
		String title = "信息提示";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userName = new String(request.getParameter("userName").getBytes("ISO-8859-1"), "UTF-8");
		String oldPassword = new String(request.getParameter("oldPassword").getBytes("ISO-8859-1"), "UTF-8");
		String newPassword = new String(request.getParameter("newPassword").getBytes("ISO-8859-1"), "UTF-8");
		String newPassword_one = new String(request.getParameter("newPassword_one").getBytes("ISO-8859-1"), "UTF-8");
		if (userName.equals("")) {
			wrong1();
			response.sendRedirect("index.jsp");
		} else if (oldPassword.equals("")) {
			wrong2();
			response.sendRedirect("index.jsp");
		} else if (oldPassword.equals("")) {
			wrong3();
			response.sendRedirect("index.jsp");
		} else if (newPassword.equals("")) {
			wrong4();
			response.sendRedirect("index.jsp");
		} else {
			try {
				Connection con = null;
				Statement stmt = null;
				Statement stmt_1 = null;
				ResultSet rs = null;
				Class.forName("com.mysql.cj.jdbc.Driver"); //// 驱动程序名
				String url = "jdbc:mysql://localhost:3306/htuser?serverTimezone=UTC "; // 数据库名
				String username = "root"; // 数据库用户名
				String password1 = "123456"; // 数据库用户密码
				con = DriverManager.getConnection(url, username, password1);
//    	                String url = "jdbc:sqlserver://localhost;databaseName=person";
//    	            	String userString="sa";
//    	                String password1="521212";
//    	                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//    		        	con=DriverManager.getConnection(url,userString,password1);
				stmt = con.createStatement();
				stmt_1=con.createStatement();
				String sql = "select * from ht_user where username='" + userName + "'";
				rs = stmt.executeQuery(sql);
				while (rs.next()) {
					if (!userName.equals(rs.getString("userName"))) {
						wrong1();
						response.sendRedirect("index.jsp");
					} else if (!oldPassword.equals(rs.getString("password"))) {
						wrong2();
						response.sendRedirect("index.jsp");
					} else if (newPassword.equals("")) {
						wrong3();
						response.sendRedirect("index.jsp");
					} else if (newPassword_one.equals("")) {
						wrong4();
						response.sendRedirect("index.jsp");
					} else {
						String sql2 = "update ht_user set password ='" + newPassword + "' where userName='" + userName
								+ "'";
						stmt_1.executeUpdate(sql2);
					}
				}
				rs.close();
				stmt.close();
				con.close();
				response.sendRedirect("login.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
